﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiLibrary.PermissionsValidation;
using SystemCenterSoa.BaseLibrary.Tools.LogTools;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Service.SystemManager;

namespace SystemCenterSoa.ApiProject.Area.SystemManager
{
    //所哟的api的这个权限
    [ApiModuleRelation("admin_user_manager")]
    public class AdminUserManagerController : SystemManagerBaseController
    {
        private AdminUserService _AdminUserService;
        public AdminUserManagerController(AdminUserService AdminUserService)
        {
            _AdminUserService = AdminUserService;
        }
        //MyBaseController过滤器加载这里了
        [HttpPost]//过滤器，面向切面。就相当于这个，不影响本身的代码
        public ResultModel<PagerResult<AdminUserPo>> SearchPage(SearchModel sm)
        {
            try
            {
                var data = _AdminUserService.SearchPage(sm);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<PagerResult<AdminUserPo>>(ex);
            }
        }
        //添加
        [HttpPost]
        [ApiModuleRelation("dishige")]//添加修改用户，是子的，所以写在这里
        public ResultModel<bool> AdminUserDtoAdd(AdminUserDto view)
        {
            try
            {
                //当前时间，这个不会出现在显示里头
                view.creator_date = DateTime.Now;
                //用户名
                view.creator_name = LoginUserDto.user_name;
                var data = _AdminUserService.AdminUserDtoAdd(view);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
        //判断输入的内容是否有重复值
        [HttpGet]
        public ResultModel<bool> VAdmminUserName(string userName)
        {
            try
            {
                var data = _AdminUserService.VAdmminUserName(userName);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpPost]
        [ApiModuleRelation("dishige")]
        public ResultModel<bool> AdminUserDtoUpdate(AdminUserDto view)
        {
            try
            {
                view.modifi_name = LoginUserDto.user_name;
                view.modifi_date = DateTime.Now;
                var data = _AdminUserService.AdminUserDtoUpdate(view);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpGet]
        public ResultModel<AdminUserPo> GetAdminUserPoByUserName(string userName)
        {
            try
            {
                var data = _AdminUserService.GetAdminUserPoByUserName(userName);
                if (data == null)
                {
                    return MyFail<AdminUserPo>("没有获取对应的用户");
                }
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<AdminUserPo>(ex);
            }
        }
        [HttpGet]
        [ApiModuleRelation("dishige")]
        public ResultModel<bool> AdminUserPoPwdUpdate(string pwd, string userName)
        {
            try
            {
                var data = _AdminUserService.AdminUserPoPwdUpdate(pwd, userName);
                if (data)
                {
                    return MyOk(data);

                }
                return MyFail<bool>("修改失败");
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]//不验证登录，就不需要验证这个意思就是有了这个就不需要进行token对比验证，否则就会出现你去买票，售票人让你出示票，才可以买票的尴尬情形。
        public ResultModel<LoginUserDto> Login(string userName, string pwd)
        {
            //throw new Exception("测试错误");
            try
            {
                //定义空字段
                string mess = "";
                var data = _AdminUserService.Login(userName, pwd, out mess);
                if (data != null)
                {
                    //如果不为空即为成功
                    Log4Tools.Info($"{userName}登录成功");
                    return MyOk(data);
                   
                }
                Log4Tools.Info($"{userName}登录失败");
                return MyFail<LoginUserDto>(mess);
            }
            catch (Exception ex)
            {
                //异常处理
                Log4Tools.Error($"{userName}登录出错", ex);
                return MyError<LoginUserDto>(ex);
            }
        }

        [HttpGet]
        [AllowAnonymous]  //不验证登录
        public ResultModel<bool> LoginOut()
        {
            try
            {
                string mess = "";
                bool data = AdminUserService.LoginOut();
                if (data)
                {
                    return MyOk(data);

                }
                return MyFail<bool>();

            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
    }
}
